package com.yonyou.pmclouds.workconnect.service.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.workConnect.entity.WorkConReceiverVO;
import com.yonyou.pmclouds.workConnect.service.rmiitf.WorkConReceiverQuery;
import com.yonyou.pmclouds.workconnect.mapper.WorkConReceiverMapper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作联系子表-接收人信息，查询接口实现类
 *
 * @author 唐焕
 * @date 2018.8.17
 */
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = WorkConReceiverQuery.class)
public class WorkConReceiverQueryImpl implements WorkConReceiverQuery {
    @Autowired
    private WorkConReceiverMapper mapper;

    @Override
    public WorkConReceiverVO queryById(String id) throws BusinessException {
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        return mapper.getById(id);
    }

    @Override
    public WorkConReceiverVO[] queryByIds(String[] ids) throws BusinessException {
        if (ArrayUtils.isEmpty(ids)) {
            return null;
        }
        return mapper.queryByIds(ids);
    }

    @Override
    public WorkConReceiverVO[] queryByWorkConnect(String pkWorkconnect) throws BusinessException {
        if (StringUtils.isEmpty(pkWorkconnect)) {
            return null;
        }
        return mapper.queryByWorkConnect(pkWorkconnect);
    }

    @Override
    public WorkConReceiverVO[] queryNotAcceptedVOs(String pkWorkconnect) throws BusinessException {
        if (StringUtils.isEmpty(pkWorkconnect)) {
            return null;
        }
        return mapper.queryNotAcceptedVOs(pkWorkconnect);
    }

    @Override
    public WorkConReceiverVO queryByPkWorkconnectAndPkReceiver(String pkWorkconnect, String pkReceiver) throws BusinessException {
        if (StringUtils.isEmpty(pkWorkconnect) || StringUtils.isEmpty(pkReceiver))
            return null;
        return mapper.queryByPkWorkconnectAndPkReceiver(pkWorkconnect, pkReceiver);
    }

    @Override
    public Map<String, List<String>> queryByPkConnects(String[] pkWorkconnects) throws BusinessException {
        if (ArrayUtils.isEmpty(pkWorkconnects))
            return null;

        WorkConReceiverVO[] workConReceiverVOs = mapper.queryByPkConnects(pkWorkconnects);

        Map<String,List<String>> receiverMap = new HashMap<String,List<String>>();

        for (WorkConReceiverVO workConReceiverVO :
                workConReceiverVOs) {
            if(receiverMap.get(workConReceiverVO.getPkWorkconnect()) == null){
                receiverMap.put(workConReceiverVO.getPkWorkconnect(),new ArrayList<String>());
            }
            receiverMap.get(workConReceiverVO.getPkWorkconnect()).add(workConReceiverVO.getPkReceiver());
        }

        return receiverMap;
    }

    @Override
    public WorkConReceiverVO[] queryByWorkConnects(String[] pkWorkconnects) throws BusinessException {

        return mapper.queryByPkConnects(pkWorkconnects);
    }
}
